package com.luzyluis.medicalcalendar.vista;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;

import com.luzyluis.medicalcalendar.R;
import com.luzyluis.medicalcalendar.controlador.MedicamentosDB;

public class MainActivity extends ActionBarActivity
{

    public static int versionBD = 1;

    protected boolean _active = true;
    protected int _splashTime = 5000; // time to display the splash screen in ms


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Thread introTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    long before = System.currentTimeMillis();
                    MedicamentosDB misMeds = new MedicamentosDB(getApplicationContext(), "MedicamentosDB", null, versionBD);
                    SQLiteDatabase db = misMeds.getWritableDatabase();
                    db.close();
                    long after = System.currentTimeMillis();
                    waited += (int)(after-before);
                    while (_active && (waited < _splashTime)) {
                        sleep(100);
                        if (_active) {
                            waited += 100;
                        }
                    }
                } catch (Exception e) {

                } finally {

                    startActivity(new Intent(MainActivity.this,
                            MenuActivity.class));
                    finish();
                }
            };
        };
        introTread.start();
    }

    private void lanzarActivity()
    {
        Intent I = new Intent(this, MenuActivity.class);
        startActivity(I);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
